﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using OrderMgr.Rpt;
using OrderMgr.CustomerInfo;
using System.Collections;
using TierApplication;

namespace OrderMgr.Rpt
{
    public partial class frmRptSaleAnalysis : TierService.frmTemplate
    {

        int iOrderNum = 0, iProductCount = 0;
        decimal dSumMoney = 0;

        public frmRptSaleAnalysis()
        {
            InitializeComponent();
            this.Text = "销售单查询";
        }

        private void initForm()
        {
            TierService.Utility.SetFeeVisible(clInSumPrice, Utility.gSeeAllFeeData);
            TierService.Utility.SetFeeVisible(clProfit, Utility.gSeeAllFeeData);

            TierService.Utility.SetFeeVisible(clPayFee, TierService.Utility.gSeeSaleFee);
            TierService.Utility.SetFeeVisible(clTotalInvoiceFee, TierService.Utility.gSeeSaleFee);
            TierService.Utility.SetFeeVisible(clTotalReturnFee, TierService.Utility.gSeeSaleFee);
            TierService.Utility.SetFeeVisible(clInvoiceRcvFee, TierService.Utility.gSeeSaleFee);
            TierService.Utility.SetFeeVisible(clOrderRcvMoney, TierService.Utility.gSeeSaleFee);

            txtOrderUserID.UserID = TierService.Utility.gUserID;
            txtOrderUserID.UserName = TierService.Utility.gUserName;

            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            //Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");
            Utility.DataToLookupComboxEx(txtCustSource, C_DMB.GetListByType(C_DMB.DType.CustSource.ToString()), "CodeID", "CodeName");

            //Utility.DataToLookupComboxEx(txtOrderStatus, C_DMB.GetListByType(C_DMB.DType.OrderStatus.ToString()), "CodeID", "CodeName");          
            //-----------ADD OrderStatus
            //DataTable dtOrderStatus = C_DMB.GetListByType(C_DMB.DType.OrderStatus.ToString()).Tables[0];
            //txtStockStatus.Properties.Items.Clear();
            //txtStockStatus.Properties.SeparatorChar = ';';
            //for (int i = 0; i < dtOrderStatus.Rows.Count; i++)
            //{
            //    string sCodeID = dtOrderStatus.Rows[i]["CodeID"].ToString();
            //    string sCodeName = dtOrderStatus.Rows[i]["CodeName"].ToString();
            //    if ((sCodeID == "3") || (sCodeID == "4"))
            //    {
            //        txtStockStatus.Properties.Items.Add(sCodeID, sCodeName, CheckState.Checked, true);
            //    }
            //    else
            //        txtStockStatus.Properties.Items.Add(sCodeID, sCodeName, CheckState.Unchecked, true);
            //}
            //-----------ADD OrderStatus            Utility.DataToLookupComboxEx(txtOrderUserID, (new P_User()).GetList(""), "UserID", "UserName");


            ckbOrderDate.Checked = true;
            txtOrderDateStart.DateTime = DateTime.Now.AddDays(-7);
            txtOrderDateEnd.DateTime = DateTime.Now.AddDays(1);


        }

        private void frmRptSale_Load(object sender, EventArgs e)
        {
            initForm();
            // P_User mUser= new P_User();
            //treeComBoxEdit1.treeList.DataSource = mUser.GetUserTreeList("001").Tables[0];



        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string ls_where = "";


            ls_where = " and a.OrderType ='" + (int)Utility.OrderType.Sale + "'";

            if ((txtOrderUserID.Text.Trim() != "") && (ckbOrderUserID.Checked))
                ls_where = ls_where + " and a.OrderUserID = '" + (string)txtOrderUserID.UserID + "'";
            if (ckbAllUser.Checked)
                ls_where = ls_where + " and exists (select 1 from dbo.f_puser('" + (string)txtOrderUserID.UserID + "') where UserID=a.OrderUserID )";

            if ((txtOrderID.Text != "") && (ckbOrderID.Checked))
                ls_where = ls_where + " and a.OrderID like '%" + txtOrderID.Text + "%'";

            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                ls_where = ls_where + " and a.DepotID = '" + (string)txtDepotID.EditValue + "'";


            //if ((txtStockStatus.Text.Trim() != "") && (ckbOrderStatus.Checked))
            //{
            //    string sOrderStatus = (string)txtStockStatus.EditValue;
            //    string sStatus = sOrderStatus.Replace(';', ',');
            //    ls_where = ls_where + " and a.OrderStatus in (" + sStatus + ")";
            //}

            string sStockStatus = (string)txtStockStatus.EditValue;
            if (sStockStatus == "0")//已出库
            {
                ls_where = ls_where + " and TotalStockedQty=TotalActQty";
            }
            else
            {
                ls_where = ls_where + " and TotalStockedQty<>TotalActQty";
            }

            if ((ckbOrderDate.Checked) && (txtOrderDateStart.Text != "") && (txtOrderDateEnd.Text != ""))
            {
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) >='" + txtOrderDateStart.DateTime.ToString("yyyy-MM-dd") + "'";
                ls_where = ls_where + " and Convert(varchar(10),a.orderdate,120) <='" + txtOrderDateEnd.DateTime.ToString("yyyy-MM-dd") + "'";
            }

            if ((txtCustID.Text.Trim() != "") && (ckbCustID.Checked))
                ls_where = ls_where + " and a.CustID = '" + txtCustID.Text + "'";

            if ((txtCustSource.Text.Trim() != "") && (ckbCustSource.Checked))
                ls_where = ls_where + " and exists (select 1 from T_Customer where CustID=a.CustID and  CustSource = '" + (string)txtCustSource.EditValue + "')";

            T_Order mOrder = new T_Order();
            DataSet dsCustomer = mOrder.GetRptSaleAnalysis(ls_where);
            gcOrder.DataSource = dsCustomer.Tables[0];

            gvOrder.BestFitColumns();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["OrderID"].ToString();

            frmRptSaleDetail fDetail = new frmRptSaleDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.Sale;
            fDetail._OrderID = sID;
            fDetail.ShowDialog();
        }



        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            btnQuery_Click(null, null);
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtCustID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmCustomer fDetail = new frmCustomer();
            fDetail.gFuncType = Utility.FuncType.Select;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtCustID.Text = fDetail._CustID;
            }
        }

    }

}